package api;

//定义泛型

import java.util.ArrayList;
import java.util.List;

public class GenericTest04 {
    //接受任意类型的list
    public static void printList(List<?> list){
        for (Object element : list){
            System.out.print(element + " ");
        }
    }
    //接受泛型是number类型或其子类的list
    public static void sumNumbers(List<? extends Number>  list){
       double sum = 0;
       for (Number number : list){
           sum += number.doubleValue();
       }
    }
    //接受泛型为string类型或者string类型父类类型的list
    public static void printStrings(List<? super String> list){
        System.out.println(list.toString());
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("haha");
        list.add("hello");
        GenericTest04.printList(list);
        List<Integer> list1 = new ArrayList<>();//haha hello
        list1.add(12);
        list1.add(15);
        GenericTest04.printList(list1);//12 15
        GenericTest04.sumNumbers(list1);//
        List<Long> list2 = new ArrayList<>();
        list2.add(100L);
        list2.add(200L);
        GenericTest04.sumNumbers(list2);
    }
}
